﻿//获取登录人员信息
function getUserInfo() {
    var url = "CommonPage.aspx?opration=getUserInfo";
    $.ajax({
        url: url,
        type: "post",
        dataType: 'json',
        success: function (data) {
            if (data.Success) {
                var count = 0;
                var str = "";
                str += "<a href=\"JavaScript:void(0)\"><i></i><label id=\"H_UserName\"></label></a>";
                str += "<ul>";
                for (var i = 0; i < data.RoleIds.split(",").length; i++) {
                    if (data.RoleIds.split(",")[i] == "1") {
                        str += "<li><a href=\"#\" onclick=\"click_LinkPage('Admin','1');\">酒店管理员</a></li>";
                        str += "<li><a href=\"#\" onclick=\"click_LinkPage('OrderQuery','1');\">订单查询</a></li>";
                        str += "<li><a href=\"#\" onclick=\"click_LinkPage('Cook','1');\">厨师(配菜等)</a></li>";
                        str += "<li><a href=\"#\" onclick=\"click_LinkPage('Senddish','1');\">传菜员</a></li>";
                        str += "<li><a href=\"#\" onclick=\"click_LinkPage('Cashier','1');\">收银员</a></li>";
                        str += "<li><a href=\"#\" onclick=\"click_LinkPage('Clean','1');\">服务员</a></li>";
                        break;
                    }
                    else if (data.RoleIds.split(",")[i] == "2") {
                        str += "<li><a href=\"#\" onclick=\"click_LinkPage('Minister','1');\">部长</a></li>";
                        str += "<li><a href=\"#\" onclick=\"click_LinkPage('OrderQuery','1');\">订单查询</a></li>";
                        str += "<li><a href=\"#\" onclick=\"click_LinkPage('Cook','1');\">厨师(配菜等)</a></li>";
                        str += "<li><a href=\"#\" onclick=\"click_LinkPage('Senddish','1');\">传菜员</a></li>";
                        str += "<li><a href=\"#\" onclick=\"click_LinkPage('Cashier','1');\">收银员</a></li>";
                        str += "<li><a href=\"#\" onclick=\"click_LinkPage('Clean','1');\">服务员</a></li>";
                        break;
                    }
                    else if (data.RoleIds.split(",")[i] == "3") {
                        count++;
                        str += "<li><a href=\"#\" onclick=\"click_LinkPage('Clean','0');\">服务员</a></li>";
                    }
                    else if (data.RoleIds.split(",")[i] == "4") {
                        count++;
                        str += "<li><a href=\"#\" onclick=\"click_LinkPage('Cashier','0');\">收银员</a></li>";
                    }
                    else if (data.RoleIds.split(",")[i] == "5") {
                        count++;
                        //str += "<li><a href=\"#\" onclick=\"click_LinkPage('Cook','0');\">厨师(配菜等)</a></li>";
                    }
                    else if (data.RoleIds.split(",")[i] == "6") {
                        count++;
                        str += "<li><a href=\"#\" onclick=\"click_LinkPage('Senddish','0');\">传菜员</a></li>";
                    }
                }
                if (count > 0) {
                    str += "<li><a href=\"#\" onclick=\"click_LinkPage('Cook','0');\">厨师(配菜等)</a></li>";
                }
                str += "<li><a href=\"#\" onclick=\"click_LinkPage('PassWord','0');\">修改密码</a></li>";
                str += "</ul>";
                $("#lilist").html(str);
                $("#H_UserName").text(data.H_UserName);
                $("#H_Name").text(data.H_Name);
                //$("#RoleName").text(data.RoleName);
            }
            else {
                updateTip(data.Msg, true, "URL", data.Url);
            }
        }
    });
}
//登录人员目录相关操作
function click_LinkPage(flag,pageFlag) {
    var url;
    if (flag == "Admin")
    {
        url = "manager.html?PageFlag=" + pageFlag;
    }
    else if (flag == "Minister") {
        url = "manager.html?PageFlag=" + pageFlag;
    }
    else if (flag == "Clean") {
        url = "clean.html?PageFlag=" + pageFlag;
    }
    else if (flag == "Cashier") {
        url = "cashier.html?PageFlag=" + pageFlag;
    }
    else if (flag == "Cook") {
        url = "cook.html?PageFlag=" + pageFlag;
    }
    else if (flag == "Senddish") {
        url = "senddish.html?PageFlag=" + pageFlag;
    }
    else if (flag == "OrderQuery")
    {
        url = "orderquery.html?PageFlag=" + pageFlag;
    }
    else if (flag == "PassWord") {
        url = "password.html?PageFlag=" + pageFlag;
    }
    document.location.href = url;
}
//获取URL参数
function getParameter(param) {
    var query = window.location.search;
    var iLen = param.length;
    var iStart = query.indexOf(param);
    if (iStart == -1)
        return "";
    iStart += iLen + 1;
    var iEnd = query.indexOf("&", iStart);
    if (iEnd == -1)
        return query.substring(iStart);
    return query.substring(iStart, iEnd);
}
//提示窗口(消息,操作是否成功,执行标志,参数)
function updateTip(msg, isTrue, flag, str) {
    $(".dialogTip").text(msg)
    $("#dialog").dialog({
        resizable: false,
        height: 120,
        modal: true,
        position: ["center", "center"],
        buttons: {
            //"关 闭": function () {
            //    $(this).dialog("close");
            //},
            "确 定": function () {
                if (isTrue) {
                    if (flag == "Method") {
                        getListData(str);
                    }
                    else if (flag == "URL") {
                        document.location.href = str;
                    }
                }
                $(this).dialog("close");
            }
        }
    });
}
//换桌
function showDineTable() {
    $("#tblDineTable").html("");
    var DineTableId = $("#hide_DineTable").val();
    var datas = "&OrderId=" + OrderId + "&DineTableId=" + DineTableId;
    var url = "CommonPage.aspx?opration=getDineTable" + datas;
    $.ajax({
        url: url,
        type: "post",
        dataType: 'json',
        success: function (data) {
            if (data.Success) {
                var str = "<tr><td class='l'>桌位类型</td><td class='c'>桌位名称</td><td class='c'>人数</td></tr>";
                for (var i = 0; i < data.List.length; i++) {
                    str += "<tr id='" + data.List[i].DineTableId + "' title='" + data.List[i].Name + "' onClick='change()' onMouseOut='out()'><td class='l'>" + data.List[i].Category + "</td><td class='c'>" + data.List[i].Name + "</td><td class='r'>" + data.List[i].Numbers + "</td></tr>";
                }
                $("#tblDineTable").html(str);

                $("#DineTable").dialog("destroy");
                $("#DineTable").dialog({
                    autoOpen: false,
                    resizable: false,
                    width: 310,
                    height: 390,
                    modal: true,
                    position: ["center", "center"],
                    buttons: {
                        "关 闭": function () {
                            $(this).dialog("close");
                        },
                        "确 定": function () {
                            updateOrderDineTable(OrderId, DineTableId);
                        }
                    }
                });

                $("#DineTable").dialog('open');
            }
            else {
                updateTip(data.Msg, false, "", "");
            }
        }
    });
}
//更换订单桌位
function updateOrderDineTable(OrderId, DineTableId) {
    var array = getChangeTD();
    if (array == "")
    {
        updateTip("请选择更换的桌位！", false, "", "");
        return;
    }
    var datas = "&OrderId=" + OrderId + "&DineTableId=" + DineTableId + "&newDineTableId=" + array.split("|")[0];
    var url = "CommonPage.aspx?opration=updateOrderDineTable" + datas;
    $.ajax({
        url: url,
        type: "post",
        dataType: 'json',
        success: function (data) {
            if (data.Success) {
                $("#DineTable").dialog('close');
                $("#Dinecondition").text(array.split("|")[1]);
                getListData(OrderId);
                //updateTip(data.Msg, true, "Method", OrderId);
            }
            else {
                updateTip(data.Msg, false, "", "");
            }
        }
    });
}
//获取选择行的数据
function getChangeTD() {
    var str = "";
    //rgb(237, 237, 237)
    //#ededed
    for (var i = 1; i < document.all.tblDineTable.rows.length; i++) {
        if (document.all.tblDineTable.rows[i].style.backgroundColor == "lightgray" || document.all.tblDineTable.rows[i].style.backgroundColor == "rgb(211, 211, 211)") {
            str = document.all.tblDineTable.rows[i].id + "|" + document.all.tblDineTable.rows[i].title;
            break;
        }
    }
    return str;
}
//鼠标点击选择行时候变色
function change() {
    var oObj = event.srcElement;
    if (oObj.tagName.toLowerCase() == "td") {
        var oTr = oObj.parentNode;
        for (var i = 1; i < document.all.tblDineTable.rows.length; i++) {
            document.all.tblDineTable.rows[i].style.backgroundColor = "";
            document.all.tblDineTable.rows[i].tag = false;
        }
        oTr.style.backgroundColor = "lightgray";
        oTr.tag = true;
    }
}
//鼠标点击另外一行时关闭已选行变色
function out() {
    var oObj = event.srcElement;
    if (oObj.tagName.toLowerCase() == "td") {
        var oTr = oObj.parentNode;
        if (!oTr.tag)
            oTr.style.backgroundColor = "";
    }
}

//减少菜品份数
function subtractNum(CategoryId, CookBookId, IsDisplay) {
    if (parseInt($("#ipt_" + CategoryId + "_" + CookBookId + "_" + IsDisplay).val()) != 0) {
        $("#ipt_" + CategoryId + "_" + CookBookId + "_" + IsDisplay).val(parseInt($("#ipt_" + CategoryId + "_" + CookBookId + "_" + IsDisplay).val()) - 1);
    }
}

//增加菜谱份数
function addNum(CategoryId, CookBookId, IsDisplay, EstimateClear) {
    var add = parseInt($("#ipt_" + CategoryId + "_" + CookBookId + "_" + IsDisplay).val()) + 1;
   // alert(EstimateClear);
    if (EstimateClear == "-1") {
        $("#ipt_" + CategoryId + "_" + CookBookId + "_" + IsDisplay).val(add);
    } else {
        if (parseInt(EstimateClear) - add < 0) {
            alert("剩余数量不足，只剩" + EstimateClear + "份");
            return false;
        } else {
            $("#ipt_" + CategoryId + "_" + CookBookId + "_" + IsDisplay).val(add);
        }
    }
}

//删除菜单,把对应菜品份数改为零.
function deleteCookbook(CategoryId, CookBookId, IsDisplay) {
    $("#ipt_" + CategoryId + "_" + CookBookId + "_" + IsDisplay).val("0");
}

//开台
function openDineTable(obj, HotelId, DineTableId, UrlFlag, PageFlag) {

    var html = "就餐人数：<input type='text' id='jcrs'  class='w100'/>"; 
    $(".dialogTip").html(html); 
    $("#dialog").dialog({
        resizable: false,
        height: 130,
        width: 300,
        modal: true,
        position: ["center", "center"],
        buttons: {
            "关 闭": function () {
                $(this).dialog("close");
            },
            "确 定": function () {
                var jcrs = $("#jcrs").val();
                if ($("#jcrs").val()=="") {
                    alert("请输入就餐人数");
                    $("#jcrs").focus();
                    return false;
                } 
                var datas = "&HotelId=" + HotelId + "&HotelName=" + encodeURIComponent($("#H_Name")[0].innerHTML) + "&DineTableId=" + DineTableId + "&Category=" + encodeURIComponent(obj.getAttribute("dir")) + "&jcrs=" + jcrs;
                var url = "CommonPage.aspx?opration=openDineTable" + datas;
                $.ajax({
                    url: url,
                    type: "post",
                    dataType: 'json',
                    success: function (data) {
                        $(this).dialog("close");
                        if (data.Success) {
                            var str = data.Url + "?HotelId=" + HotelId + "&OrderId=" + data.OrderId + "&Name=" + encodeURIComponent(obj.getAttribute("name")) + "&BookedDate=" + encodeURIComponent(data.BookedDate) + "&UrlFlag=" + UrlFlag + "&PageFlag=" + PageFlag;
                            //updateTip(data.Msg, true, "URL", str);
                            document.location.href = str;
                        }
                        else {
                            updateTip(data.Msg, false, "", "");
                        }
                    }
                });
            }
        }
    });
}